//
//  OnlineConsultingController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/25.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "OnlineConsultingController.h"
#import "HomeCell.h"
#import "SeekController.h"
#import "RchatConteroller.h"
#import <RongIMKit/RongIMKit.h>
#import "CellRCConversationViewController.h"
#import "DataManager.h"
#import "SeekModel.h"
#import <UIImageView+WebCache.h>
#import <MBProgressHUD.h>
#import "LoginViewController.h"

#define kShared [DataManager defaultManager]

@interface OnlineConsultingController ()
@property(nonatomic,strong)NSMutableArray *arrayT;



@end

@implementation OnlineConsultingController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        [kShared getSeekArrayAndBlock:^{
            [self.tableView reloadData];
        }];
    });
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"在线咨询";
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"消息" style:(UIBarButtonItemStylePlain) target:self action:@selector(newSelector)];
    
    //注册 cell
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil]   forCellReuseIdentifier:@"cell"];
    
    
    
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 400)];
    view.backgroundColor = [UIColor whiteColor];
    
    
    self.arrayT = @[@"1",@"2",@"3",@"4"].mutableCopy;
    
    
    //第三方的轮播图
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 300) imageNamesGroup:self.arrayT];
    
    [view addSubview:cycleScrollView];
    
    self.tableView.tableHeaderView = view;
    
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    searchBtn.backgroundColor = [UIColor clearColor];
    [searchBtn setTitle:@"搜索医生" forState:UIControlStateNormal];
    
    searchBtn.frame = CGRectMake(0, 300, CGRectGetWidth(self.view.bounds), 100);
    [view addSubview:searchBtn];
    [searchBtn addTarget:self action:@selector(didClickSearchBtn:) forControlEvents:UIControlEventTouchDown];
    
}
//搜索医生的点击事件
- (void)didClickSearchBtn:(UIButton *)sender{
    
    SeekController *sc = [[SeekController alloc] init];
    [self.navigationController pushViewController:sc animated:YES];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"名医堂";

}



//消息页面的点击事件
- (void)newSelector{
    
    if([kShared.userType isEqualToString:@"普通用户"]||[kShared.userType isEqualToString:@"医生"])
    {
        RchatConteroller  *con = [[RchatConteroller alloc] init];
        
        [self.navigationController pushViewController:con animated:YES];
    }
    else
    {
        [self addAlert:@"请先登陆才能查看消息"];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    return kShared.seekArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SeekModel *seek = kShared.seekArray[indexPath.row];
    cell.nameLable.text = seek.name;
    cell.zhichenglabel.text = seek.zhicheng;
    cell.yiyuanLabel.text = seek.yiyuan;
    cell.keLeiLabel.text = seek.kelei;
    AVFile *file = seek.currentUser[@"Picture"];
    [cell.imaV sd_setImageWithURL:[NSURL URLWithString:file.url]];
    
    return cell;
}
//跳转到聊天页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([kShared.userType isEqualToString:@"普通用户"]||[kShared.userType isEqualToString:@"医生"])
    {
        SeekModel *seek = kShared.seekArray[indexPath.row];
        CellRCConversationViewController *cc = [[CellRCConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:seek.currentUser.username];
        cc.seek = seek;
        cc.titleName = seek.name;
        [self.navigationController pushViewController:cc animated:YES];
    }else
    {
        [self addAlert:@"请先登陆才能咨询医生"];
    }
    
    
}

//弹框
- (void)addAlert:(NSString *)message
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *isOK = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    [alert addAction:isOK];
    [self presentViewController:alert animated:YES completion:nil];
}



//懒加载
- (NSMutableArray *)arrayT{
    
    if (_arrayT == nil) {
        _arrayT = [NSMutableArray array];
        
        
        
    }
    
    return _arrayT;
    
}

@end
